import auth from '../../../api/auth'
export default{
    data(){
        return {
            defaultProps: {
                children: 'menus',
                label: 'cdmc',
                isLeaf: 'leaf'
            },
            menus:[]
        }
    },
    methods:{
        onTreeNodeClick(data,node,tree){
        },
        onSaveAuth(){

            if(!this.selectedRole)
                return;
            let keys = this.$refs.menuTree.getCheckedKeys();
            auth.saveMenu({
                bh:this.selectedRole.jsbh,
                ids:keys
            })
            .then(response => {
                const { data } = response;
                if( data.error_code == 0 ){
                    this.showInfo("保存成功！");
                }else{
                    this.showError("保存失败！");
                }
            }).catch(error => {
                this.showError("本地异常！");
            });
        },
        getNodeIcon(node){

            if( node.isLeaf ){
                if( node.data.icon ){
                    return <i class={node.data.icon}></i>
                }
                else{
                    return <i class="rs-icon-file-fill0"></i>
                }
            }else{
                if( node.expanded ){
                    return [<i class="rs-icon-folder-open-fill"></i>,<i class={node.data.icon}></i>]
                }else{
                    return [<i class="rs-icon-folder-fill"></i>,<i class={node.data.icon}></i>]
                }
            }
        },
        renderContent(h, { node, data, store }) {

            return (
            <div class={{"rs-tree-node":true,"leaf":node.isLeaf}}>
                {this.getNodeIcon(node)}
                <span class="label">{node.label}</span>
            </div>);
        },
        loadMenus(){
            auth.queryMenu({
                jsbh:this.selectedRole.jsbh
            })
            .then(response => {
                this.loading = false;
                const { data } = response;
                if( data.error_code == 0 ){
                    
                    let keys = data.menus.map((item)=>item.cdbh)
                    this.$refs.menuTree.setCheckedKeys(keys);
                }else{
                }
            }).catch(error => {
                this.loading = false;
            });
        },
        loadMenuTree(){
            auth.queryMenuTree()
            .then(response => {
                this.loading = false;
                const { data } = response;
                if( data.error_code == 0 ){
                    
                    this.menus = data.menus;
                }else{
                }
            }).catch(error => {
                this.loading = false;
            });
        },
    }
}